﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Logic.ClarizenSvc;
using Logic.ClarizenObjects;

namespace Logic.ClarizenObjects
{
    public class WorkItem: BaseObject
    {
        public string Name { get; set; }
        public string Description { get; set; }
        [Clarizen(Editable = true, Selectable = true)]
        public GenericEntity State { get; set; }
        [Clarizen(Editable = false, Selectable = true)]
        public GenericEntity Parent { get; set; }
        public GenericEntity ParentProject { get; set; }
        [Clarizen(Editable=false,Selectable=true)]
        public String Parent_Name { get { return Parent != null && Parent.Values != null && Parent.Values.Length == 1 ? Parent.Values[0].Value.ToString() : ""; } }
        [Clarizen(Editable = false, Selectable = true)]
        public String ParentProject_Name { get { return ParentProject != null && ParentProject.Values.Length == 1 ? ParentProject.Values[0].Value.ToString() : ""; } }
        public GenericEntity TrackStatus { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? DueDate { get; set; }
        [Clarizen(Editable = false, Selectable = true)]
        public int NumberOfDirectSubItems { get; set; }

        //[Clarizen(Editable = false, Selectable = true)]
        public GenericEntity Importance { get; set; }
        [Clarizen(Editable = false, Selectable = true)]
        public String SYSID {get;set;}

        public GenericEntity Manager { get; set; }

        [Clarizen(Editable = false, Selectable = true)]
        public String Manager_Name { get { return Manager != null && Manager.Values != null && Manager.Values.Length == 1 ? Manager.Values[0].Value.ToString() : ""; } }

        [Clarizen(Editable = true, Selectable = true)]
        public Duration RemainingEffort { get; set; }
        public Duration ActualEffort { get; set; }
        public Duration Duration { get; set; }
        public Duration Work { get; set; }
        public double? PercentCompleted { get; set; }
        [Clarizen(Editable = false, Selectable = true)]
        public Boolean ActualEffortUpdatedFromTimesheets { get; set; }
        [Clarizen(Editable = false, Selectable = true)]
        public Boolean IndividualReporting { get; set; }

        [Clarizen]
        public List<RegularResourceLink> Resources { get; set; }

        [Clarizen]
        public String StartDateString
        {
            get { return StartDate.HasValue? StartDate.Value.ToString("yyyy-MM-dd"): null; }
            set { StartDate = Convert.ToDateTime(value); }
        }
        [Clarizen]
        public String DueDateString
        {
            get { return DueDate.HasValue ? DueDate.Value.ToString("yyyy-MM-dd") : null; }
            set { DueDate = Convert.ToDateTime(value); }
        }

        [Clarizen]
        public String ParentID
        {
            set { this.Parent = new WorkItem { ID = new EntityId { TypeName = "WorkItem", Value = value } }.ToGenericEntity(); }
            get { return this.Parent != null && this.Parent.Id != null ? this.Parent.Id.Value : null; }
        }

        [Clarizen]
        public String StateID
        {
            set { this.State = new GenericEntity { Id = new EntityId { TypeName = "State", Value = value } }; }
            get { return this.State != null && this.State.Id != null ? this.State.Id.Value : null; }
        }

        [Clarizen]
        public String TrackStatusID
        {
            set { this.TrackStatus = new GenericEntity { Id = new EntityId { TypeName = "TrackStatus", Value = value } }; }
            get { return this.TrackStatus != null && this.TrackStatus.Id != null ? this.TrackStatus.Id.Value : null; }
        }

        [Clarizen]
        public String ImportanceID
        {
            set { this.Importance = new GenericEntity { Id = new EntityId { TypeName = "Importance", Value = value } }; }
            get { return this.Importance != null && this.Importance.Id != null ? this.Importance.Id.Value : null; }
        }

        [Clarizen]
        public String ManagerID
        {
            set { if(!string.IsNullOrEmpty(value)) this.Manager = new GenericEntity { Id = new EntityId { TypeName = "User", Value = value } }; }
            get { return this.Manager != null && this.Manager.Id != null ? this.Manager.Id.Value : null; }
        }

        public WorkItem(GenericEntity entity)
            : base(entity)
        {
            
        }

        public WorkItem()
        {
        }

        public IEnumerable<Document> GetDocuments()
        {
            var fields = new List<String>(BaseObject.GetFields(typeof(AttachmentLink)));
            fields.AddRange(BaseObject.GetFields(typeof(Document)).Select(f=> "Document." + f));

            var attachments = new QueryComponent().ExecuteQuery(new EntityQuery 
                                    {
                                        TypeName = "AttachmentLink", 
                                        Where = new Compare 
                                        { 
                                            LeftExpression = new FieldExpression{ FieldName = "Entity" }, 
                                            Operator = Operator.Equal, 
                                            RightExpression = new ConstantExpression{ Value = this.ID} 
                                        },
                                        Fields = fields.ToArray()
                                    }).Select(e => 
                                        {
                                            var doc = BaseObject.Intitialize<Document>(BaseObject.Intitialize<AttachmentLink>(e).Document);
                                            doc.LinkID = e.Id.Value;
                                            return doc;
                                        });

            return attachments;

        }
    }
}